//
//  MYComposeTextView.swift
//  NewMicroblog
//
//  Created by apple on 2016/11/1.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MYComposeTextView: UITextView {
    //MARK:- 添加空间label
    fileprivate lazy var placeHolderlabel: UILabel = {
        let label = UILabel()
        label.text = "听说我的老婆很漂亮，我觉得也是"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.lightGray
        label.numberOfLines = 0
        return label
    }()
    ///提供占位文字属性
    var placeHolder: String?{
        didSet{
            placeHolderlabel.text = placeHolder
        }
    }
    //重写fontd的属性方法
    override var font: UIFont?{
        didSet{
            if font != nil {
                placeHolderlabel.font = font!
            }
        }
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
//        placeHolderlabel.isEnabled = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   //MARK:- 搭建界面
    fileprivate func setupUI(){
        addSubview(placeHolderlabel)
        //监听文字的改变通知
        NotificationCenter.default.addObserver(self, selector: #selector(textChang), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
        placeHolderlabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeHolderlabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: placeHolderlabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 6))
        addConstraint(NSLayoutConstraint(item: placeHolderlabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: -10))
    }
    
    /// j监听文字改变的通知方法
    @objc private func textChang(){
        placeHolderlabel.isHidden = hasText
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

}
